<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所在班级" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生名字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模拟面试" prop="simulationInterviewStatusId">
        <el-select v-model="queryParams.simulationInterviewStatusId" placeholder="请选择模拟面试" clearable>
          <el-option
            v-for="dict in dict.type.it_simulation_interview_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="结业考试" prop="completionExamStatusId">
        <el-select v-model="queryParams.completionExamStatusId" placeholder="请选择结业考试" clearable>
          <el-option
            v-for="dict in dict.type.it_completion_exam_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['eduadmin:studentes:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['eduadmin:studentes:remove']"
        >不关注</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['eduadmin:studentes:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        type="index"
        width="50">
      </el-table-column>
      <el-table-column label="id" align="center" v-if="false" width="35" prop="id" />
      <el-table-column label="学生名字" align="center" prop="name" />
      <el-table-column label="所属班级" align="center" prop="itClasses.className" />
      <el-table-column label="负责老师" align="center" prop="itFocus.teacherName" />
      <el-table-column label="困难系数" align="center" prop="itFocus.difficultyId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.it_difficulty" :value="scope.row.itFocus.difficultyId"/>
        </template>
      </el-table-column>
      <el-table-column label="模拟面试" align="center" prop="simulationInterviewStatusId">
        <template slot-scope="scope">
          <el-select v-model="scope.row.simulationInterviewStatusId" placeholder="请选择模拟面试" style="width:100%" clearable
                     @change="handleStudentStatusChange(scope.row)"
          >
            <el-option
              v-for="dict in dict.type.it_simulation_interview_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            >
              <span style="color: #f8ac59">{{ dict.label }}</span>
            </el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="结业考试" align="center" prop="completionExamStatusId">
        <template slot-scope="scope">
          <el-select v-model="scope.row.completionExamStatusId" placeholder="请选择结业考试" style="width:100%" clearable
                     @change="handleStudentStatusChange(scope.row)"
          >
            <el-option
              v-for="dict in dict.type.it_completion_exam_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            >
              <span style="color: #f8ac59">{{ dict.label }}</span>
            </el-option>
          </el-select>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleFollow(scope.row)"
            v-hasPermi="['eduadmin:studentes:edit']"
          >跟踪记录</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['eduadmin:studentes:edit']"
          >编辑任务</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['eduadmin:studentes:remove']"
          >移除留级</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入名字" />
        </el-form-item>
        <el-form-item label="性别" prop="sexId">
          <el-select v-model="form.sexId" placeholder="请选择学生性别" style="width:100%" clearable>
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="班级" prop="studentStatusId">
          <el-select v-model="form.classId" placeholder="请选择班级" style="width:100%" clearable>
            <el-option
              v-for="dict in unschedul"
              :key="dict.id"
              :label="dict.className"
              :value="dict.id"
            >
              <span style="float: left">{{ dict.className }}</span>
              <span v-if="dict.stateId == 1" style="float: right; color: #8492a6; font-size: 13px">未开班</span>
              <span v-if="dict.stateId == 2" style="float: right; color: #8492a6; font-size: 13px">已开班</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="学历" prop="education">
          <el-input v-model="form.education" placeholder="请输入学历" />
        </el-form-item>
        <el-form-item label="学校" prop="school">
          <el-input v-model="form.school" placeholder="请输入学校" />
        </el-form-item>
        <el-form-item label="专业" prop="major">
          <el-input v-model="form.major" placeholder="请输入专业" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="家长电话" prop="parentPhone">
          <el-input v-model="form.parentPhone" placeholder="请输入家长电话" />
        </el-form-item>
        <el-form-item label="学生状态" prop="studentStatusId">
          <el-select v-model="form.studentStatusId" placeholder="请选择学生状态" style="width:100%" clearable>
            <el-option
              v-for="dict in dict.type.it_student_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模拟面试" prop="simulationInterviewStatusId">
          <el-select v-model="form.simulationInterviewStatusId" placeholder="请选择模拟面试" style="width:100%" clearable>
            <el-option
              v-for="dict in dict.type.it_simulation_interview_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结业考试" prop="completionExamStatusId">
          <el-select v-model="form.completionExamStatusId" placeholder="请选择结业考试" style="width:100%" clearable>
            <el-option
              v-for="dict in dict.type.it_completion_exam_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 跟踪历史弹窗 -->
    <el-dialog :title="followHistoryTitle" :visible.sync="followHistoryOpen" width="500px" append-to-body>
      <div class="block">
        <el-timeline>
          <el-timeline-item
            v-for="(activity, index) in followHistory"
            :key="index"
            :color="activity.color"
            :timestamp="activity.timeOccurrence" placement="top">
            <el-card>
              <h4>跟踪人 [{{activity.teacherName}}]</h4>
              <p>情况说明: {{activity.problemDescription}}</p>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStudentes,
  getStudentes,
  delStudentes,
  addStudentes,
  updateStudentes,
  changeState,
  changeClass,
  changeFocus,
  addFollow
} from "@/api/eduadmin/studentes";
import { getClassByState } from "@/api/eduadmin/class";
import {getProblemByStudentId} from "@/api/eduadmin/problem";
import {getUserProfile} from "@/api/system/user";

export default {
  name: "Repetition",
  dicts: ['it_student_status', 'sys_user_sex', 'it_problem_type','it_campus', 'it_simulation_interview_status', 'it_completion_exam_status', 'it_assistant_teacher', 'it_difficulty'],
  data() {
    return {
      //性别下拉框
      sexOptions: [
        {
          value: 1,
          label: "男"
        },
        {
          value: 2,
          label: "女"
        }
      ],
      //已开班班级
      unschedul: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 状态数组
      states: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生管理表格数据
      studentesList: [],
      // 弹出层标题
      title: "",
      detailsTitle: "",
      followHistoryTitle: "",
      // 是否显示弹出层
      open: false,
      detailsOpen: false,
      followHistoryOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        sexId: null,
        age: null,
        classId: null,
        education: null,
        school: null,
        major: null,
        phone: null,
        parentPhone: null,
        studentStatusId: null,
        simulationInterviewStatusId: null,
        completionExamStatusId: null,
        isRepetition: 1
      },
      // 表单参数
      form: {},
      detailsForm: {},
      followHistory:[],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名字不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "电话不能为空", trigger: "blur" }
        ],
        parentPhone: [
          { required: true, message: "家长电话不能为空", trigger: "blur" }
        ],
      },
      userInfo: {}
    };
  },
  created() {
    this.getList();
    this.handleGetMyinfo();
  },
  methods: {
    // 获取学生管理列表
    handleStudentStatusChange(student) {
      this.form = student
      updateStudentes(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },
    /** 查询学生管理列表 */
    getList() {
      this.loading = true;
      listStudentes(this.queryParams).then(response => {
        this.studentesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 根据班级状态查询班级 */
    listUnschedul(states) {
      getClassByState(states).then(response =>  {
        this.unschedul = response.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.detailsOpen = false;
      this.followHistoryOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        sexId: null,
        age: null,
        classId: null,
        education: null,
        school: null,
        major: null,
        phone: null,
        parentPhone: null,
        studentStatusId: null,
        simulationInterviewStatusId: null,
        completionExamStatusId: null
      };
      this.resetForm("form");
      this.resetForm("detailsForm");
      this.resetForm("followHistoryForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.states = [1,2];
      this.listUnschedul(this.states);
      const id = row.id || this.ids
      getStudentes(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生管理";
      });
    },

    /**
     * 查看详情
     *
     * @param row
     */
    handleDetails(row) {
      this.reset();
      this.states = [1,2,3];
      this.listUnschedul(this.states);
      getStudentes(row.id).then(response => {
        this.detailsForm = response.data;
        this.detailsOpen = true;
        this.detailsTitle = "学生详情";
      });
    },

    /**
     * 查看跟踪记录
     * @param row
     */
    handleFollow(row) {
      getStudentes(row.id).then(response => {
        this.followHistoryOpen = true;
        this.followHistoryTitle = response.data.name + "  的跟踪历史";
      });
      getProblemByStudentId(row.id).then(response => {
        if (response.data.length > 0) {
          this.followHistory = response.data;
          for (let i = 0; i < this.followHistory.length; i++) {
            if (i == 0) {
              //第一条数据颜色为红色
              this.followHistory[i].color="#FF0000"
            }
            if (i == 1) {
              //第二条数据颜色为绿色
              this.followHistory[i].color="#00FF00"
            }
          }
        } else {
          this.followHistory = [];
        }
      });
    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudentes(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudentes(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },


    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学生管理编号为"' + ids + '"的数据项？').then(function() {
        return delStudentes(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('student/studentes/export', {
        ...this.queryParams
      }, `studentes_${new Date().getTime()}.xlsx`)
    },

    handleGetMyinfo() {
      //var userName = this.$store.state.user.name;
      getUserProfile().then(response => {
        this.myInfo = response.data;
        //console.log(this.myInfo);
      });
    },
  }
};
</script>
